import requests as rq
import lxml.html

header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'} #伪装浏览器
url = 'https://www.kugou.com/yy/rank/home/1-8888.html?from=rank'
sources = rq.get(url,headers = header)
type(sources)
data= sources.text #网页源代码
selector=lxml.html.fromstring(data)
'''
#获取单个数据的时候用normalize-space 去掉空格
#获取排名
rank = selector.xpath('normalize-space(//*[@id="rankWrap"]/div[2]/ul/li[1]/span[3]/strong/text())')
#获取歌名
song = selector.xpath('normalize-space(//*[@id="rankWrap"]/div[2]/ul/li[1]/a/text())')
#获取歌星名称
singer = selector.xpath('normalize-space(//*[@id="rankWrap"]/div[2]/ul/li[1]/a/span/text())')
print(rank,song,singer)
'''


#获取排名前三的排名序号
rank1 = selector.xpath('//*[@id="rankWrap"]/div[2]/ul/li/span/strong/text()')
print(rank1)
#获取第四名开始至后面的排名序号
rank2 = selector.xpath('//*[@id="rankWrap"]/div[2]/ul/li/span/text()')
rank2 = ''.join(rank2) #列表转化为字符串
rank2 = rank2.split() #去掉空格
print(rank2)
rank = rank1+rank2 #所有排名
#获取歌名
song = selector.xpath('//*[@id="rankWrap"]/div[2]/ul/li/a/text()')
song = ''.join(song).split()
#获取歌星名称
singer = selector.xpath('//*[@id="rankWrap"]/div[2]/ul/li/a/span/text()')
print(singer)

import pandas as pd
D=pd.DataFrame({'榜单排名':rank,'歌名':song,'歌星':singer})

